#include "menu.h"
#include <allegro.h>
#include <vector>
#include <stdio.h>
using namespace std;

menu::menu(char *file) {
   this->selected = 0;
   this->arrow = load_bitmap("media/seta.bmp", NULL);
   char bmp[150];
   char temp_text[150];
   int temp_w, temp_h;
   int temp_x, temp_y;
   FILE *pmenu = fopen(file, "r");
   fscanf(pmenu,"%s", bmp);
   fscanf(pmenu,"%d %d", &temp_w, &temp_h);
   while (!feof(pmenu)) {
      fscanf(pmenu, "%d %d %s", &temp_x, &temp_y, temp_text);
      if(feof(pmenu))
         break;
      this->options.push_back(new option(temp_x, temp_y, temp_text));
   }
   this->w = temp_w;
   this->h = temp_h;
   this->background = load_bmp(bmp, NULL);
}

void menu::draw(BITMAP *buffer) {
   if(this->w == 0 && this->h == 0) {

      masked_stretch_blit(this->background, buffer, 0, 0, this->background->w,
                          this->background->h, 0, 0, SCREEN_W, SCREEN_H);

      if (options.size() == 0)
         return;
      for(int i = 0; i < options.size(); i++)
         options[i]->draw(buffer);

   } else {

      masked_stretch_blit(this->background, buffer, 0, 0, this->background->w,
                          this->background->h, (SCREEN_W - this->w)/2,
                          (SCREEN_H - this->h)/2, this->w, this->h);

      if (options.size() == 0)
         return;
      for(int i = 0; i < options.size(); i++)
         options[i]->draw(buffer);
      }
      draw_sprite(buffer, arrow, (options[selected]->x - arrow->w - 5), (options[selected]->y - arrow->h/2));
}

void menu::move(int direction) {
   if (direction == 1) {
      
      if(selected == (options.size() - 1))
         selected = 0;
      else
         selected++;

   } else if (direction == 0) {
      
      if(selected == 0)
         selected = options.size() - 1;
      else
         selected--;

   }
}

